package day05;

import java.util.Scanner;

public class WuZiQi {
	static char[][] panal = new char[16][16];

	public static void init() {
		for (int i = 0; i < panal.length; i++) {
			for (int j = 0; j < panal[i].length; j++) {
				if (j == 0 && i == 0) {
					panal[i][j] = ' ';
				} else if (i == 0) {
					if (j > 9) {
						panal[i][j] = (char) (97 + j - 10);
						continue;
					} else {
						panal[i][j] = (char) (j + 48);
					}
				} else if (j == 0) {
					if (i > 9) {
						panal[i][j] = (char) (97 + i - 10);
						continue;
					} else {
						panal[i][j] = (char) (i + 48);
					}
				} else {
					panal[i][j] = '*';
				}
			}
		}
	}

	public static void printChess() {
		for (int i = 0; i < panal.length; i++) {
			for (int j = 0; j < panal[i].length; j++) {
				System.out.print(" " + panal[i][j]);
			}
			System.out.println();
		}
	}

	public static int fromCharToInt(char ch) {
		int temp = 0;
		if (ch <= '9' && ch >= '1') {
			temp = ch - 48;
		} else if (ch <= 'f' && ch >= 'a') {
			temp = ch + 10 - 97;
		} else {
			System.out.println("输入出错");
		}
		return temp;
	}

	public static boolean wasWin(int r, int c) {
		return wasWinAtV(r, c) || wasWinAtH(r, c) || wasWinAtLD(r, c)
				|| wasWinAtRD(r, c);
	}
/**
 * 横方向
 * @param r
 * @param c
 * @return
 */
	public static boolean wasWinAtV(int r, int c) {
		boolean b = false;
		int num = 1;
		int i = 1;
		int k = 0;
		while (true) {
			if (k == 0) {
				if (c-i>0&&panal[r][c - i] == panal[r ][c ]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else if (k == 1) {
				if (c+i<16&&panal[r][c + i] == panal[r ][c]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else {
				break;
			}
		}
		if (num == 5) {
			b = true;
		}
		return b;
	}
/**
 * 竖方向
 * @param r
 * @param c
 * @return
 */
	public static boolean wasWinAtH(int r, int c) {
		boolean b = false;
		int num = 1;
		int i = 1;
		int k = 0;
		while (true) {
			if (k == 0) {
				if (r-i>0&&panal[r-i][c] == panal[r ][c ]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else if (k == 1) {
				if (r+i<16&&panal[r+i][c] == panal[r][c]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else {
				break;
			}
		}
		if (num == 5) {
			b = true;
		}
		return b;
	}
/**
 * 正斜方向
 * @param r
 * @param c
 * @return
 */
	public static boolean wasWinAtLD(int r, int c) {
		boolean b = false;
		int num = 1;
		int i = 1;
		int k = 0;
		while (true) {
			if (k == 0) {
				if (r-i>0&&c-i>0&&panal[r-i][c-i] == panal[r ][c ]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else if (k == 1) {
				if (r+i<16&&c+i<16&&panal[r+i][c+i] == panal[r][c]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else {
				break;
			}
		}
		if (num == 5) {
			b = true;
		}
		return b;
	}
/**
 * 反斜方向
 * @param r
 * @param c
 * @return
 */
	public static boolean wasWinAtRD(int r, int c) {
		boolean b = false;
		int num = 0;
		int i = 1;
		int k = 0;
		while (true) {
			if (k == 0) {
				if (r-i>0&&c+i<16&&panal[r-i][c+i] == panal[r ][c ]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else if (k == 1) {
				if (r+i<16&&c-i>0&&panal[r+i][c-i] == panal[r][c]) {
					i++;
					num++;
				} else {
					k++;
					i=1;
				}
			} else {
				break;
			}
		}
		if (num == 5) {
			b = true;
		}
		return b;
	}

	public static void main(String[] args) {
		System.out.println("\t\t五子棋游戏");
		init();
		boolean isBlack = true;
		Scanner s = new Scanner(System.in);
		printChess();
		while (true) {
			System.out.println("请" + (isBlack ? "黑" : "白") + "方落子");
			String str = s.next();
			int r = fromCharToInt(str.charAt(0));
			int c = fromCharToInt(str.charAt(1));
			if (panal[r][c] != '*') {
				System.out.println("该位置已经有棋子，请重新落子");
				continue;
			} else {
				panal[r][c] = isBlack ? '@' : 'O';
				printChess();
				if (wasWin(r, c)) {
					System.out.println((isBlack ? "黑" : "白") + "方获胜！");
					break;
				}
			}
			isBlack = !isBlack;

		}

	}

}
